/**
 * \file ctrl_mem.c
 * \brief Controle memoire flash.
 * \author {Mengxuan.Z Nicolas.M}
 * \version 0.15
 * \date 12 mars 2011
 *
 * Fichier d'impl��mentation des fonctions du controle memoire flash.
 *
 */

#include "ctrl_mem.h"

#include "../driver/flashMemory.h"

#include <io.h>
#include <signal.h>
#include <iomacros.h>
#include <stdio.h>
#include <stdlib.h>

/**
 *\def		SEG_A           
 *\def      SEG_B  
 *\def      STATION_PERSO 
 *\def      STATION_STAT 
 *\def      VOL_STAT  
 *\def      STATION_COUR 
 *\def      VOL_COUR 
 *\def      MODE_COUR  
 */
#define SEG_A (char*)0x1080
#define SEG_B (char*)0x1000
#define STATION_PERSO (char*)0x1080
#define STATION_STAT (char*)0x108a
#define VOL_STAT (char*)0x10ae
#define STATION_COUR (char*)0x10b2
#define VOL_COUR (char*)0x10b4
#define MODE_COUR (char*)0x10b6

unsigned int memoryContent[29];
unsigned int *userStations;
unsigned int *stationsStats;
unsigned int *volStats; 
unsigned int *stationPtr;
unsigned int *freqPtr;
unsigned int *volumePtr;
unsigned int *modePtr;

void initMemory()
{
	readMemory();
	userStations = &memoryContent[0];
	stationsStats = &memoryContent[5];
	volStats = &memoryContent[23]; 
	stationPtr = &memoryContent[25];
	freqPtr = &memoryContent[26];
	volumePtr = &memoryContent[27];
	modePtr = &memoryContent[28];
	
	// Reset au premier lancement du prog
	if (*modePtr == 0xffff) resetMemory();
}

void readMemory()
{
	int i = 0;
	char *ptr = SEG_A;
	for (i=0;i<29;i++)
	{
		memoryContent[i] = readUINT(ptr);
		ptr = ptr+2;
	}
}

void saveMemory()
{
	int i = 0;
	unsigned int val = 0;
	char temp[58];
	for (i=0;i<29;i++)
	{
		val = memoryContent[i];
		temp[i*2] = val&0x00ff;
		temp[i*2+1] = (val>> 8)&0x00ff;
	} 
	write_flashMemory (SEG_A,temp,58); 
}

void razMemory ()
{
  clean_flashMemory();
}

void resetMemory()
{
	int i=0;
	while(i<29)
	{
		memoryContent[i++] = 0;
	}
	saveMemory();
}


void saveStationPerso (unsigned int numStation, unsigned int val)
{
  userStations[numStation-1] = val;
  saveMemory();
}

unsigned int getStationPerso (unsigned int numStation)
{
    unsigned int freq;
    freq = userStations[numStation-1];
    return freq;
}

void saveStationTimeStat (unsigned int numStation, unsigned int seconds)
{
        stationsStats[numStation-1] = seconds;
}

unsigned int getStationTimeStat (unsigned int numStation)
{
	unsigned int val = 0;

        val = stationsStats[numStation-1];
	return val;
}

void saveVolTimeStat (unsigned int vol, unsigned int seconds)
{
	if (vol>=7 && vol<=8)
		volStats[1]=seconds;
	else if (vol>=1 && vol<=6)
		volStats[0]=seconds;
}

unsigned int getVolTimeStat (unsigned int vol)
{
	unsigned int val = 0;
	if (vol>=7 && vol<=8)
		val = volStats[1];
	else if (vol>=1 && vol<=6)
		val = volStats[0];
	return val;
}

void augmenterStationTimeStat (unsigned int numStation,unsigned int timeToAdd)
{
	unsigned int savedTime = getStationTimeStat (numStation);
	savedTime = savedTime+timeToAdd;
	saveStationTimeStat (numStation, savedTime);
}

void augmenterVolTimeStat (unsigned int vol,unsigned int timeToAdd)
{
	unsigned int savedTime = getVolTimeStat (vol);
	savedTime = savedTime+timeToAdd;
	saveVolTimeStat (vol, savedTime);
}

void saveStation(unsigned int numStation)
{
	*stationPtr = numStation;
}

unsigned int getSavedStation()
{
	unsigned int val = 0;
	val = *stationPtr;
	return val;
}

void saveFrequence(unsigned int frequence)
{
	*freqPtr = frequence;
}

unsigned int getSavedFreq()
{
	unsigned int val = 0;
	val = *freqPtr;
	return val;
}

void saveVolume(unsigned int vol)
{
	*volumePtr = vol;
}

unsigned int getSavedVol()
{
	unsigned int val = 0;
	val = *volumePtr;
	return val;
}

void saveMode (unsigned int mode)
{
	*modePtr = mode;
}

unsigned int getSavedMode()
{
	unsigned int val = 0;
	val = *modePtr;
	return val;
}

